/*
 *   Copyright (c) 2010 Sonrisa Informatikai Kft. All Rights Reserved.
 * 
 *  This software is the confidential and proprietary information of
 *  Sonrisa Informatikai Kft. ("Confidential Information").
 *  You shall not disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Sonrisa.
 * 
 *  SONRISA MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 *  THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 *  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SONRISA SHALL NOT BE LIABLE FOR
 *  ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 *  DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.zh.jbet.web.components.datatable;

import com.zh.jbet.core.exception.ExceptionCodesEnum;
import com.zh.jbet.core.exception.SystemException;
import com.zh.jbet.web.pages.BasePanel;
import java.lang.reflect.ParameterizedType;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.IFilterStateLocator;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author sonrisa
 */
public abstract class DataTablePanel<R> extends BasePanel {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataTablePanel.class);
    private static final String TABLE_ID = "datatable";
    private JBetDataDable<R> dataTable;
//    protected String className;

    public DataTablePanel(String id, TableColumnDescriptor[] columDescs, ISortableDataProvider<R> dataProvider, int rowsPerPage) {
        super(id, null);
        dataTable = new JBetDataDable(TABLE_ID, createTableColumns(columDescs), dataProvider, rowsPerPage);
        FilterForm form = new FilterForm(
                "filterform", (IFilterStateLocator)dataProvider);
        dataTable.addTopToolbar(
                new FilterToolbar(dataTable, (FilterForm) form, (IFilterStateLocator)dataProvider));
        form.add(dataTable);
        add(form);
       //this.clazz = DataTablePanel.class;
    }

/*
    protected String getRowClassName() {
        ParameterizedType genericSuperclass = (ParameterizedType)getClass().getGenericSuperclass();
        return ((Class)genericSuperclass.getActualTypeArguments()[0]).getSimpleName();
    }
*/
    protected IColumn<R>[] createTableColumns(TableColumnDescriptor[] descs) {
        IColumn<R>[] ret = new IColumn[descs.length+1];
        int idx = 0;
        for (TableColumnDescriptor desc: descs) {
            ret[idx++] = createColumn(desc);
        }
        ret[idx] = new RowLinkColumn(createRowDisplayModel(), createRowLinkModel(), null) {
            @Override
            protected void onRowSelect(AjaxRequestTarget target, IModel rowModel) {
                onRowSelected(target, rowModel);
            }
        };
        return ret;
    }

    protected void onRowSelected(AjaxRequestTarget target, IModel<R> rowModel) {
        getJBetPage().changePanel(getDetailMenuConfig(), rowModel, DataTablePanel.this.getClass(), target);
    }

    protected IModel<String> createRowDisplayModel() {
        return null;
    }
    protected IModel<String> createRowLinkModel() {
        return null;
    }

    @Override
    public Class<? extends BasePanel>[] getMainMenuConfig() {
        return null;
    }

    public Class<? extends BasePanel>[] getDetailMenuConfig() {
        return super.getMainMenuConfig();
    }

    public JBetDataDable<R> getDataTable() {
        return dataTable;
    }

    public IDataProvider<R> getDataProvider() {
        return dataTable.getDataProvider();
    }
    
    protected IColumn<R> createColumn(final TableColumnDescriptor desc) {
        String labelKey = desc.getProperty();
        IModel<String> lmodel = new StringResourceModel(labelKey, null, labelKey);
        final String prop = desc.getProperty();
        String sort = desc.isSortable() ? prop : null;
        switch (desc.getType()) {
            case PROPERTY:
                return new PropertyColumn(lmodel, sort, prop) {
                    @Override
                    public String getCssClass() {
                        return prop + "Col";
                    }
                };
            case DATE_PROPERTY:
                return new DatePropertyColumn(lmodel, sort, prop) {
                    @Override
                    public String getCssClass() {
                        return prop + "Col";
                    }
                };
            case FILTERED: {
                if (desc.getEnumChoice() != null) {
                    return new EnumChoiceFilteredPropertyColumn(
                            lmodel, sort, prop, desc.getEnumChoice());
                } else if(desc.getServiceProvider() != null) {
                    return new EntityChoiceFilteredPropertyColumn(
                            lmodel, sort, prop, desc.getServiceProvider());
                }
                else {
                    return new K11TextFilteredPropertyColumn(
                            lmodel, sort, prop, true);
                }
            }
            default:
                throw new SystemException(ExceptionCodesEnum.Unsupported, "Not found column definition type " + desc.getType());
        }
    }
}
